package com.ww.springboot.es.controller;


import cn.hutool.json.JSONUtil;
import com.ww.springboot.es.app.search.service.IEsSearchToolSvc;
import com.ww.springboot.es.dto.EsQueryDTO;
import com.ww.springboot.es.dto.ForecastRecord;
import com.ww.springboot.es.dto.ForecastRecordDTO;
import com.ww.springboot.es.resp.ForecastRecordRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * es消息-》controller
 *
 * @author wanwei
 * @since 2021-01-26 11:10
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("es")
public class EsController {

    private final ForecastRecordRepository repository;
    private final IEsSearchToolSvc esSearchToolSvc;

    @RequestMapping("/list")
    public String create(@RequestParam(name = "page", required = false, defaultValue = "0") Integer page,
                         @RequestParam(name = "size", required = false, defaultValue = "10") Integer size,
                         ForecastRecordDTO forecastRecord) {
        return JSONUtil.toJsonStr(esSearchToolSvc.demo(PageRequest.of(page, size), forecastRecord));
    }

    @PostMapping("/exportWithApi")
    public void exportWithApi(HttpServletResponse response, @RequestBody EsQueryDTO esQueryDTO) throws IOException {
        esSearchToolSvc.exportWithApi(response, esQueryDTO);
    }

    @PostMapping("/exportWithApiAndScroll")
    public void exportWithApiAndScroll(HttpServletResponse response, @RequestBody EsQueryDTO esQueryDTO) throws IOException {
        esSearchToolSvc.exportWithApiAndScroll(response, esQueryDTO);
    }


    @PostMapping("/exportWithHttp")
    public void exportWithHttp(HttpServletResponse response, @RequestBody EsQueryDTO esQueryDTO) throws IOException {
        esSearchToolSvc.exportWithHttp(response, esQueryDTO);
    }

    @PostMapping("/exportWithHttpAggs")
    public void exportWithHttpHandAggs(HttpServletResponse response, @RequestBody EsQueryDTO esQueryDTO) throws IOException {
        esSearchToolSvc.exportWithHttpAggs(response, esQueryDTO);
    }


    @RequestMapping("/write")
    public String white(@RequestBody ForecastRecord forecastRecord) {
        // 分页参数
        repository.save(forecastRecord);
        return "成功";
    }
}
